package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.ArticleFormDto;
import cn.livingCloud.entity.content.Article;
import cn.livingCloud.entity.user.UserAccount;
import cn.livingCloud.repository.ArticleMapper;
import cn.livingCloud.utils.SessionUtils;
import com.alibaba.druid.util.StringUtils;

/**
 * Created by DingJie  on 2016/1/15.
 */
public class ArticleFormDtoUpdater {
    protected transient ArticleMapper mapper = BeanProvider.getBean(ArticleMapper.class);
    private ArticleFormDto formDto;


    public ArticleFormDtoUpdater(ArticleFormDto formDto) {
        this.formDto = formDto;
    }

    public void update() {
        String uuid = formDto.getUuid();
        if (StringUtils.isEmpty(uuid)) {
            UserAccount account = (UserAccount) SessionUtils.getCurrentUserAccount();
            Article article = new Article();
            article.setUserId(account.getUser().getUuid());
            formDto.update(article);
            mapper.save(article);
        } else {
            Article article = mapper.findByUuid(uuid);
            formDto.update(article);
            mapper.update(article);
        }
    }
}
